/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Servidor.Controlador;

import Servidor.Modelo.Games.BoardPlaying;
import Servidor.Modelo.Games.Game;
import Servidor.Modelo.Games.GameList;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Observable;
import java.util.Observer;

/**
 * This class creates a Thread for join a user to a existent game
 * @author Jorge Perez
 */
class JoinGameTask extends Thread implements OcaTask, Observer {

    private PrintWriter write;
    private BufferedReader read;
    private Socket socket;
    private int idGame;

    public JoinGameTask(int idGame, Socket socket, BufferedReader read, PrintWriter write) {
        this.write = write;
        this.socket = socket;
        this.read = read;
        this.idGame = idGame;
    }

    public JoinGameTask(Socket socket, BufferedReader read, PrintWriter write) {
        this.write = write;
        this.socket = socket;
        this.read = read;
        this.idGame = -100;
    }

    @Override
    public void executeTask() {
        this.start();
    }

    @Override
    public void run() {
        String idGameString = null;

        int idUser = -1;
        String idPlayer = null;
        try {
            idPlayer = read.readLine();

        } catch (IOException ex) {
            System.out.println("Problemas al recibir la id del jugador a unirse");
        }
        try {
            idUser = Integer.parseInt(idPlayer);
        } catch (NumberFormatException ex) {
            System.out.println("La id del jugador recibida no es un numero");
        }

        String idGame = null;
        int idGameInt = -100;

        try {
            idGame = read.readLine();

        } catch (IOException ex) {
            System.out.println("Problemas al recibir la id de la partida a unirse");
        }
        try {
            idGameInt = Integer.parseInt(idGame);
        } catch (NumberFormatException ex) {
            System.out.println("La id de la partida recibida no es un numero");
        }


        Game game = GameList.getInstance().getGame(idGameInt);

        int positionInTheGame = game.add_player_id(idUser);
        game.addObserver(this);
        write.println(positionInTheGame);
    }

    @Override
    public void update(Observable o, Object arg) {
        write.println(ConnectionConstraints.GAME_UPDATE);

        Game game = (Game)o;
        BoardPlaying tableGame = (BoardPlaying) arg;
        String infoGame;
        infoGame = String.valueOf(game.getId_game()) + "#";
        infoGame += tableGame.getPlayerTurn() + "#";
        infoGame += tableGame.getRound() + "#";
        infoGame += tableGame.getRoll_dice1() + "#";
        infoGame += tableGame.getRoll_dice2() + "#";
        for (int i = 1; i <= 8; i++) {
            infoGame += tableGame.get_position_player(i) + "#";
        }
        String message = tableGame.getMessage();
        infoGame += message;
        write.println(infoGame);
    }
}
